<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input size="default" style="max-width: 180px" v-model="tableData.param.keys"></el-input>
				
				<el-button size="default" type="success"  class="ml10" @click="onOpenAddRole">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button>
			</div>

			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="code" label="角色标识" show-overflow-tooltip></el-table-column>
				<el-table-column prop="remark" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.name === '超级管理员'" size="small" text type="primary" @click="onOpenEditRole(scope.row)"
							>修改</el-button>
						<el-button :disabled="scope.row.name === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				v-model:current-page="tableData.param.page"
				background
				v-model:page-size="tableData.param.size"
				layout="total, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddRole ref="addRoleRef" />
		<EditRole ref="editRoleRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent ,watchEffect} from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import AddRole from '/@/views/system/role/component/addRole.vue'
import EditRole from '/@/views/system/role/component/editRole.vue'
import { rolesApi } from '/@/api/system/role'
// import { pageInfo } from '/@/utils/table'

// 定义接口来定义对象的类型
interface TableData {
	name: string
	code: string
	remark: string
	row_num: number
	createtime: string
}
interface TableDataState {
	tableData: {
		data: Array<TableData>
		total: number
		loading: boolean
		param: {
			page: number
			size: number
			keys: string
		}
	}
}

export default defineComponent({
	name: 'systemRole',
	components: { AddRole, EditRole },
	setup() {
		const addRoleRef = ref()
		const editRoleRef = ref()
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					//...pageInfo,
					page: 1,
					size: 5,
					keys : ""
				},
			},
		})
		
		// 加载列表
		const dataList = async() => {
			return await rolesApi().dataList(state.tableData.param)
		}

		// 删除
		const deleteRole = async(param) => {
			return await rolesApi().delete(param)
		}

		// 初始化表格数据
		const initTableData = async() => {
			const ds = await dataList()
			const { data , total } = {...ds}
			state.tableData.data = data
			state.tableData.total = total
		}

		// 打开新增角色弹窗
		const onOpenAddRole = () => {
			addRoleRef.value.openDialog()
		}

		// 打开修改角色弹窗
		const onOpenEditRole = (row: Object) => {
			editRoleRef.value.openDialog(row)
		}

		// 删除角色
		const onRowDel = (row: any) => {

			
			ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.name}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then( async() => {
					const {code,msg} = await deleteRole(row)
					if ( code == 0 ) {
						ElMessage.success('删除成功')
						initTableData()
					} else {
						ElMessage.error(msg)
					}
				})
				.catch(() => {})
		}
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.size = val
		}
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.page = val
			console.log(val)
		}
		// 页面加载时
		onMounted(() => {
			initTableData()
		})

	  watchEffect((oninvalidate)=>{
			state.tableData.param.page
			oninvalidate(()=>{
				console.log('before')
			})
			initTableData()
  	})

		return {
			addRoleRef,
			editRoleRef,
			onOpenAddRole,
			onOpenEditRole,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			...toRefs(state),
		}
	},
})
</script>
